﻿using System;
using System.IO;
using System.Windows.Forms;
using DocumentManager.Abstractions.Factories;
using DocumentManager.Abstractions.Interfaces.Helpers;

namespace DocumentManager.Forms
{
	public partial class frmDocumentViewer : Form
	{
		#region Constructor

		public frmDocumentViewer(IWordDocumentAdapter wordDocumentAdapter, FormBaseFactory formFactory, string wordDocumentFilePath)
		{
			InitializeComponent();

			if (string.IsNullOrWhiteSpace(wordDocumentFilePath) || !File.Exists(wordDocumentFilePath))
				throw new FileNotFoundException(wordDocumentFilePath);

			if (wordDocumentAdapter == null)
				throw new ArgumentNullException(string.Format("wordDocumentAdapter"));

			if (formFactory == null)
				throw new ArgumentNullException(string.Format("formFactory"));

			var newXPSDocumentFilePath = Path.Combine(
				Path.GetDirectoryName(wordDocumentFilePath),
				string.Concat(Path.GetFileNameWithoutExtension(wordDocumentFilePath), ".xps"));
			var xpsDocument = wordDocumentAdapter.ConvertWordDocumentToXPSDocument(wordDocumentFilePath, newXPSDocumentFilePath);

			var userControl = formFactory.CreateDocumentViewerUserControl(xpsDocument);
			if (userControl == null)
				throw new ArgumentNullException(string.Format("DocumentViewerUserControl"));

			ehDocuments.Child = userControl;
		}

		#endregion
	}
}
